package com.hsgene.goods.dto;

import com.hsgene.common.util.basic.JsonUtils;
import com.hsgene.common.util.date.TimeJsonDeserializer;
import com.hsgene.common.util.validation.FieldChecker;
import com.hsgene.goods.constants.DetectionEnum;
import com.hsgene.goods.constants.GoodsOriginStatus;
import com.hsgene.goods.domain.GoodsOrigin;
import com.hsgene.goods.dto.specification.GoodsSpecificationDto;
import org.apache.commons.lang.StringUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 商品原始信息（供应商商品信息）Dto
 *
 * @author wangbing
 * @version 1.0, 2018/7/13
 */
public class GoodsOriginDto implements Serializable {

    private static final long serialVersionUID = -1234574046198296896L;
    /**
     * 商品编号
     */
    private String id;

    /**
     * 检测机构(supplier_detection_organization)id
     */
    private Long supplierDoId;

    /**
     * 套餐名称
     */
    private String goodsName;

    /**
     * 封面图
     */
    private String coverUrl = "http://xxxx";

    /**
     * 套餐商品banner介绍
     */
    private List<GoodsBannerDto> goodsBanners;

    /**
     * 原始价格
     */
    private BigDecimal detectionOriginalPrice;

    /**
     * 检测时长
     */
    private Short detectionDuration;

    /**
     * 规格包装
     */
    private List<GoodsSpecificationDto> specification;

    /**
     * 产品介绍（富文本）
     */
    private String presentation;

    /**
     * 发布时间
     */
    @JsonDeserialize(using = TimeJsonDeserializer.class)
    private Date publishTime;

    /**
     * 状态：待完善、待发布、已发布、修改申请中、上架中、已下架
     */
    private Integer status;

    /**
     * 状态描述
     */
    private String statusDesc;
    /**
     * 商品检测相关信息Map（key：detection_type = 检测类型：肿瘤，detection_locus = 检测位点（如：EGFR），sample_mode = 采样方式（如：唾液、血液、蜡片），detection_mode = 检测方式（如：芯片、二代测序））
     */
    private Map<String, List<GoodsDetectionDto>> detectionMap;

    /**
     * 创建时间
     */
    @JsonDeserialize(using = TimeJsonDeserializer.class)
    private Date createTime = new Date();

    /**
     * 更新时间
     */
    @JsonDeserialize(using = TimeJsonDeserializer.class)
    private Date updateTime;

    /**
     * 商品快照id
     */
    private String snapshootId;

    public GoodsOriginDto() {
    }

    public GoodsOriginDto(GoodsOrigin origin) {
        this.id = origin.getId();
        this.supplierDoId = origin.getSupplierDoId();
        this.goodsName = origin.getGoodsName();
        this.coverUrl = origin.getCoverUrl();
        if (StringUtils.isNotEmpty(origin.getGoodsBanner())) {
            this.goodsBanners = JsonUtils.jsonToObject(origin.getGoodsBanner(), new TypeReference<List<GoodsBannerDto>>() {
            });
        }
        this.detectionOriginalPrice = origin.getDetectionOriginalPrice();
        this.detectionDuration = origin.getDetectionDuration();
        if (StringUtils.isNotBlank(origin.getSpecification())) {
            try {
                this.specification = JsonUtils.jsonToObject(origin.getSpecification(), new TypeReference<List<GoodsSpecificationDto>>() {
                });
            } catch (Exception e) {
                this.specification = null;
            }
        }
        this.presentation = origin.getPresentation();
        this.publishTime = origin.getPublishTime();
        this.status = origin.getStatus();
        this.statusDesc = GoodsOriginStatus.getEnum(origin.getStatus()).getDesc();
        this.createTime = origin.getCreateTime();
        this.updateTime = origin.getUpdateTime();
        this.snapshootId = origin.getSnapshootId();
    }

    public void check() {
        FieldChecker.checkEmpty(goodsName, "套餐名称");
        FieldChecker.checkEmpty(coverUrl, "套餐封面图");
        FieldChecker.checkEmpty(detectionOriginalPrice, "价格");
        FieldChecker.checkEmpty(detectionDuration, "工作日");
        FieldChecker.checkEmpty(detectionMap, "检测类型");
        FieldChecker.checkEmpty(
                detectionMap.get(DetectionEnum.DETECTION_TYPE.getValue()), "检测类型");
        FieldChecker.checkEmpty(
                detectionMap.get(DetectionEnum.DETECTION_LOCUS.getValue()), "检测位点");
        FieldChecker.checkEmpty(
                detectionMap.get(DetectionEnum.SAMPLE_MODE.getValue()), "采样方式");
        FieldChecker.checkEmpty(
                detectionMap.get(DetectionEnum.DETECTION_MODE.getValue()), "检测方式");
        FieldChecker.checkEmpty(specification, "产品规格");
        FieldChecker.checkEmpty(this.getPresentation(), "产品介绍");
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Long getSupplierDoId() {
        return supplierDoId;
    }

    public void setSupplierDoId(Long supplierDoId) {
        this.supplierDoId = supplierDoId;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public String getCoverUrl() {
        return coverUrl;
    }

    public void setCoverUrl(String coverUrl) {
        this.coverUrl = coverUrl;
    }

    public List<GoodsBannerDto> getGoodsBanners() {
        return goodsBanners;
    }

    public void setGoodsBanners(List<GoodsBannerDto> goodsBanners) {
        this.goodsBanners = goodsBanners;
    }

    public BigDecimal getDetectionOriginalPrice() {
        return detectionOriginalPrice;
    }

    public void setDetectionOriginalPrice(BigDecimal detectionOriginalPrice) {
        this.detectionOriginalPrice = detectionOriginalPrice;
    }

    public Short getDetectionDuration() {
        return detectionDuration;
    }

    public void setDetectionDuration(Short detectionDuration) {
        this.detectionDuration = detectionDuration;
    }

    public List<GoodsSpecificationDto> getSpecification() {
        return specification;
    }

    public void setSpecification(List<GoodsSpecificationDto> specification) {
        this.specification = specification;
    }

    public String getPresentation() {
        return presentation;
    }

    public void setPresentation(String presentation) {
        this.presentation = presentation;
    }

    public Date getPublishTime() {
        return publishTime;
    }

    public void setPublishTime(Date publishTime) {
        this.publishTime = publishTime;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Map<String, List<GoodsDetectionDto>> getDetectionMap() {
        return detectionMap;
    }

    public void setDetectionMap(Map<String, List<GoodsDetectionDto>> detectionMap) {
        this.detectionMap = detectionMap;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public String getStatusDesc() {
        return statusDesc;
    }

    public void setStatusDesc(String statusDesc) {
        this.statusDesc = statusDesc;
    }

    public String getSnapshootId() {
        return snapshootId;
    }

    public void setSnapshootId(String snapshootId) {
        this.snapshootId = snapshootId;
    }
}
